﻿package com.eitb.programas.olentzero09.objetos{
	import flash.events.*;
	import flash.display.*;
	
	import com.eitb.util.*;
	import com.eitb.programas.olentzero09.*;
	import com.eitb.programas.olentzero09.events.*;
	
	public class Gozilla extends MovieClip {
		private var sentido:Boolean;
		private var velocidad:Number;
		private var limites:Object;
		

		public function Gozilla():void {
			// false: derecha -> izquierda; true: izquierda -> derecha
			this.sentido = Math.round(Math.random()) == 0 ? true : false;
			this.velocidad = sentido ? -(.2+Math.random()*1.25) : (.2+Math.random()*1.25);
			this.addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
		}
		
		public function initLimites(limites:Object):void {
			this.limites = limites;
		}
		
		public function init():void {
			sentido ? this.x = this.limites.xMax + 20: this.x = this.limites.x - 20;
			sentido ? this.scaleX = 1 : this.scaleX = -1;
			this.y = this.limites.yMax - 45;
		}
		
		public function onEnterFrame(evt:Event):void {
			this.x+=this.velocidad;
			if (this.isOut()) {
				this.finalize();
			}
		}
		
		private function isOut():Boolean {
			return (this.x < this.limites.x - 20) || (this.x > this.limites.xMax +20);
		}
		
		public function finalize():void {
			try {
				this.removeEventListener(Event.ENTER_FRAME, this.onEnterFrame);
				this.parent.removeChild(this);
			} catch (e:Error) {
			}
		}
		
		public function colision():void {
			this.parent.dispatchEvent(new OlentzeroEvent(OlentzeroEvent.COLISION, {colision:"enemigo", x:this.x , y:this.y}));
			this.finalize();
		}
	}
}